%  Calculate migration matrices (Cohort and Duration) with simulations of rating history by
%  bootstrap.m 

clc
clear all

ITERATION = 200;
labels = {1,2,3,4,5,6,7,8};
nRatings = length(labels);
cohStd = zeros(ITERATION, nRatings, nRatings);
durStd = zeros(ITERATION, nRatings, nRatings);

for i= 1:ITERATION
openfile = strcat('D:\My Documents\CIRANO\db_mySQL\bootstrap_08\boot_', num2str(i),'.mat');
load (openfile, 'bootData');
bootData(:,4)=[];
rows_to_remove = any(bootData==99,2);
bootData(rows_to_remove,:) = [];
results = num2cell(bootData);

[cohTp, s1, i1] = transprobwr(results, 'labels',labels,'algorithm','cohort');
[durTp, s2, i2] = transprobwr(results, 'labels',labels,'algorithm','duration');

cohTpStd.totalsVec = sum(s1.totalsMat(1:nRatings,1:nRatings),2)';
cohTpStd.totalsMat = s1.totalsMat(1:nRatings,1:nRatings);
cohTpStd.algorithm = s1.algorithm;
cohStd (i,:,:)= transprobbytotals(cohTpStd);


durTpStd.totalsVec = s2.totalsVec(1:nRatings);
durTpStd.totalsMat = s2.totalsMat(1:nRatings,1:nRatings);
durTpStd.algorithm = s2.algorithm;
durStd(i,:,:)= transprobbytotals(durTpStd);

clear bootData results

disp(i);
save ('bootstrap_cal_cohDur_08_case2.mat','cohStd','durStd')
end
 

